package com.example.yc23.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yc23.demo.pojo.Review;
import com.example.yc23.demo.service.ReviewService;
import com.example.yc23.demo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/admin/review")
public class ReviewController {
    @Autowired
    private ReviewService service;

@GetMapping({"/pass/{rid}","/unpass/{rid}"})
public String check(@PathVariable Integer rid, HttpServletRequest request){
    Integer state=null;
    if (request.getRequestURI().contains("/pass")){
        state=1;
    }
    if (request.getRequestURI().contains("/unpass")){
        state=2;
    }
    UpdateWrapper wrapper=new UpdateWrapper();
    wrapper.eq("id",rid);
    wrapper.set("state",state);
    service.update(wrapper);
    return "redirect:/admin/review/index";
}


    /**
     * 跳转评论页面   同时查询评论信息
     * @param state  评论状态   可以根据状态查找
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String index(Integer state, Model model,
                        @RequestParam(defaultValue = "1") Integer pn,
                        @RequestParam(defaultValue ="5" ) Integer ps){
        QueryWrapper wrapper=null;
        Page<Review> page=new Page<>(pn,ps);
        if (state!=null){
            wrapper=new QueryWrapper();
            wrapper.eq("state",state);
        }
        //使用mp的方式
//        QueryWrapper wrapper=null;
//        if (state!=null){
//            wrapper=new QueryWrapper();
//            wrapper.eq("state",state);
//        }
//        model.addAttribute("reviewList",service.list(wrapper));
        //自己定义的方法
//        model.addAttribute("reviewList",service.list(state));
        model.addAttribute("page",service.page(page,wrapper));
        return "admin/review/index";
    }



}
